package dz.chuxin.app.sample;

import android.content.Intent;
import android.view.View;

import com.jakewharton.rxbinding.view.RxView;

import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import dz.chuxin.app.R;
import dz.chuxin.app.core.BaseActivity;
import dz.chuxin.app.core.ConstantValues;
import dz.chuxin.app.sample.douban.DouBanMovieActivity;
import rx.functions.Action1;

/**
 * App项目主界面
 * Created by chuxin on 2016/6/30.
 */
public class HomeActivity extends BaseActivity {

    @Override
    protected void setUpContentView() {
        setContentView(R.layout.activity_home, R.string.text_home, -1, ConstantValues.ACTIVITY_MODE_DRAWER);
        ButterKnife.bind(this);
        findViewById(R.id.profile_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(HomeActivity.this, ProfileActivity.class));
            }
        });
        findViewById(R.id.row_view_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(HomeActivity.this, RowViewActivity.class));
            }
        });
        RxView.clicks(findViewById(R.id.rxjava_retrofit_button))
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        startActivity(new Intent(HomeActivity.this, DouBanMovieActivity.class));
                    }
                });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        int action = intent.getIntExtra(ConstantValues.KEY_HOME_ACTION, ConstantValues.ACTION_BACK_TO_HOME);
        switch (action) {
            case ConstantValues.ACTION_KICK_OUT:
                break;
            case ConstantValues.ACTION_LOGOUT:
                break;
            case ConstantValues.ACTION_RESTART_APP:
                protectApp();
                break;
            case ConstantValues.ACTION_BACK_TO_HOME:
                break;
        }
    }

    @Override
    protected void protectApp() {
        startActivity(new Intent(this, WelcomeActivity.class));
        finish();
    }
}
